/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChangePassWord.java
 *
 * Created on 23/07/2011, 03:36:36 ص
 */
package com.face;

import com.sql.ChangePassSql;
import java.util.Arrays;
import javax.swing.ImageIcon;

/**
 *
 * @author Khaled Lela
 */
public class ChangePassWord extends javax.swing.JFrame {

    /** Creates new form ChangePassWord */
    public ChangePassWord() {
        initComponents();
        setIconImage(new ImageIcon(getClass().getResource("/images/changePass.png")).getImage());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        oldPassWord = new javax.swing.JPasswordField();
        newPass = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        repeatedPass = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        changePass = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("تغيير كلمة المرور");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Traditional Arabic", 0, 24));
        jLabel1.setForeground(new java.awt.Color(0, 51, 204));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("تعديل كلمة المرور");

        oldPassWord.setFont(new java.awt.Font("Tahoma", 0, 24));
        oldPassWord.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        newPass.setFont(new java.awt.Font("Tahoma", 0, 24));
        newPass.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel3.setFont(new java.awt.Font("Traditional Arabic", 0, 24));
        jLabel3.setForeground(new java.awt.Color(51, 0, 255));
        jLabel3.setText("كلمة المرور القديمة");

        jLabel4.setFont(new java.awt.Font("Traditional Arabic", 0, 24));
        jLabel4.setForeground(new java.awt.Color(51, 0, 255));
        jLabel4.setText("كلمة المرور الجديدة");

        repeatedPass.setFont(new java.awt.Font("Tahoma", 0, 24));
        repeatedPass.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel5.setFont(new java.awt.Font("Traditional Arabic", 0, 24));
        jLabel5.setForeground(new java.awt.Color(51, 0, 255));
        jLabel5.setText("كرر كلمة المرور");

        changePass.setFont(new java.awt.Font("Traditional Arabic", 0, 24));
        changePass.setForeground(new java.awt.Color(51, 0, 255));
        changePass.setText("موافق");
        changePass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changePassActionPerformed(evt);
            }
        });

        exitButton.setFont(new java.awt.Font("Traditional Arabic", 0, 24));
        exitButton.setForeground(new java.awt.Color(51, 0, 255));
        exitButton.setText("خروج");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(140, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(208, 208, 208))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(exitButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 109, Short.MAX_VALUE)
                        .addComponent(changePass))
                    .addComponent(repeatedPass, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                    .addComponent(newPass, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                    .addComponent(oldPassWord, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel3))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(oldPassWord, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(newPass, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(repeatedPass, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(changePass)
                    .addComponent(exitButton))
                .addContainerGap(42, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 481, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void changePassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changePassActionPerformed
        //old pass enter by user
        char[] oldpassuser = oldPassWord.getPassword();
        // old pass saved in DB in String format
        String oldPassDB = new ChangePassSql().getOldPass();
        // convert old pass get from DB in char array format
        char[] oldPassDbArray = oldPassDB.toCharArray();
        // check old pass which enter by user true or Not
        if (!Arrays.equals(oldpassuser, oldPassDbArray)) {
            javax.swing.JOptionPane.showMessageDialog(rootPane, "كلمة المرور القديمة التى أدخلتها غير صحيحة", "خطأ", javax.swing.JOptionPane.ERROR_MESSAGE);
        } else {
            char[] passWord = newPass.getPassword();
            char[] repeated = repeatedPass.getPassword();
            if (!Arrays.equals(passWord, repeated)) {
                javax.swing.JOptionPane.showMessageDialog(rootPane, "كلمة المرور الجديدة غير متطابقة", "خطأ", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            } else {
                new ChangePassSql().setPass(String.valueOf(passWord));
                dispose();
            }
        }
}//GEN-LAST:event_changePassActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        dispose();
}//GEN-LAST:event_exitButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton changePass;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField newPass;
    private javax.swing.JPasswordField oldPassWord;
    private javax.swing.JPasswordField repeatedPass;
    // End of variables declaration//GEN-END:variables
}
